/**
 * 本质上就是计算子树的尺寸，然后做个二分
 */
#include <bits/stdc++.h>
using namespace std;

#include <bits/extc++.h>
using namespace __gnu_pbds;

using llt = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

int N;
vvi G;
int Q;
vi D;

void proc(){
    D.reserve(N);
    queue<int> q;
    q.push(1);
    D.push_back(1);

    int sz;
    while(sz = q.size()){
        while(sz--){
            auto h = q.front(); q.pop();
            for(auto v : G[h]) q.push(v);
        }
        D.emplace_back(D.back() + q.size());
    }
    return;
}

int proc(int x){
    auto p = upper_bound(D.begin(), D.end(), x);
    return *--p;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("z.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(0);
    int nofkase = 1;
    // cin >> nofkase;
    while(nofkase--){
        cin >> N;
        G.assign(N + 1, vi());
        for(int p,i=1;i<=N;++i){
            cin >> p;
            if(p) G[p].push_back(i);
        }
        proc();
        cin >> Q;
        for(int a,q=1;q<=Q;++q){
            cin >> a;
            cout << proc(a) << endl;
        }
    }
    return 0;
}